#include "demo_v1_User.h"
#include <drogon/drogon.h>
#include <string>
#include <stdlib.h>
#include <dirent.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace drogon;
using namespace demo::v1;
// add definition of your processing function here

void User::getImg(const HttpRequestPtr &req,
                  std::function<void(const HttpResponsePtr &)> &&callback,
                  std::string ip, std::string ftpmethod, std::string path, std::string filename)
{
    auto resp = HttpResponse::newHttpResponse();
    resp->setStatusCode(k200OK);
    resp->setContentTypeCode(CT_TEXT_HTML);
    resp->addHeader("Access-Control-Allow-Origin", "*");
    DIR *dir;
    char querypath[40] = "/mnt/img";
    if ((dir = opendir(querypath)) == NULL)
    {
        resp->setBody("false");
    }
    else
    {
        char systemcmd[256] = "cd /home/ej/deletefile && ./a.out ";
        strcat(systemcmd, ip.c_str());
        strcat(systemcmd, " ");
        strcat(systemcmd, ftpmethod.c_str());
        strcat(systemcmd, " ");
        strcat(systemcmd, path.c_str());
        strcat(systemcmd, " ");
        strcat(systemcmd, filename.c_str());
        if (system(systemcmd) != -1)
        {
            resp->setBody("true");
        }
        else
        {
            resp->setBody("false");
        }
    }
    callback(resp);
}
void User::getAllIpAddr(const HttpRequestPtr &req,
                        std::function<void(const HttpResponsePtr &)> &&callback) const
{
    std::string ret = "";
    std::string tail = ",";
    auto resp = HttpResponse::newHttpResponse();
    auto clientPtr = drogon::app().getDbClient();
    auto f = clientPtr->execSqlAsyncFuture("select * from ipaddr");
    auto r = f.get(); // Block until we get the result or catch the exception;
    // std::cout << r.size() << " rows selected!" << std::endl;
    int i = 0;
    for (auto row : r)
    {
        // std::cout << i++ << ": ip is " <<     row["ip"].as<std::string>() << std::endl;
        // ret["ip"] = row["ip"].as<std::string>();
        ret = ret + row["ip"].as<std::string>() + tail;
    }
    resp->setStatusCode(k200OK);
    resp->setContentTypeCode(CT_TEXT_HTML);
    resp->addHeader("Access-Control-Allow-Origin", "*");
    resp->setBody(ret);
    callback(resp);
}

void User::camerastate(const HttpRequestPtr &req,
                       std::function<void(const HttpResponsePtr &)> &&callback,
                       std::string reqipaddr)
{
    char requrl[100];
    sprintf(requrl, "http://%s:8080", reqipaddr.c_str());
    // 创建HttpRequest目标
    auto requ = HttpRequest::newHttpRequest();
    requ->setMethod(Get);
    requ->setPath("/get_camera_state/");
    // requ->setParameter("city","南宁");
    // requ->setParameter("key","52cbe165b187ce56dd385f02d4dd0be2");

    // 创建一个client目标
    auto client = HttpClient::newHttpClient(requrl);
    client->sendRequest(requ, [callback](ReqResult result, const HttpResponsePtr &response)
                        {
        if (result == ReqResult::Ok)
        {
            auto body=response->getBody();
            std::string resdata=body.data();
            auto resp = HttpResponse::newHttpResponse();
            resp->setStatusCode(k200OK);
            resp->setContentTypeCode(CT_TEXT_HTML);
            resp->addHeader("Access-Control-Allow-Origin", "*");
            resp->setBody(resdata);
            callback(resp);
        }
        else
        {
            auto body =result;
            std::string resdata=body.data();
            auto resp=HttpResponse::newHttpResponse();
            resp->setStatusCode(k200OK);
            resp->setContentTypeCode(CT_TEXT_HTML);
            resp->addHeader("Access-Control-Allow-Origin", "*");
            resp->setBody(resdata);
            callback(resp);
        } });
    //创建响应体
    
}

void User::contactcamera(const HttpRequestPtr &req,
                         std::function<void(const HttpResponsePtr &)> &&callback)
{
    auto resp = HttpResponse::newHttpResponse();
    resp->setStatusCode(k200OK);
    resp->setContentTypeCode(CT_TEXT_HTML);
    resp->addHeader("Access-Control-Allow-Origin", "*");
    resp->setBody("连接成功");
    callback(resp);
}

void User::poweroffcamera(const HttpRequestPtr &req,
                          std::function<void(const HttpResponsePtr &)> &&callback)
{
    auto resp = HttpResponse::newHttpResponse();
    resp->setStatusCode(k200OK);
    resp->setContentTypeCode(CT_TEXT_HTML);
    resp->addHeader("Access-Control-Allow-Origin", "*");
    resp->setBody("true");
    callback(resp);
}

void User::lsmedia(const HttpRequestPtr &req,
                   std::function<void(const HttpResponsePtr &)> &&callback)
{
    Json::Value ret;
    ret["文件名1"] = "文件名1";
    ret["文件名2"] = "文件名2";
    ret["文件名3"] = "文件名3";
    ret["文件名4"] = "文件名4";
    auto resp = HttpResponse::newHttpJsonResponse(ret);
    resp->setStatusCode(k200OK);
    resp->setContentTypeCode(CT_TEXT_HTML);
    resp->addHeader("Access-Control-Allow-Origin", "*");

    callback(resp);
}

void User::listfilesdetail(const HttpRequestPtr &req,
                           std::function<void(const HttpResponsePtr &)> &&callback,
                           std::string path)
{
    auto resp = HttpResponse::newHttpResponse();
    std::string resdata = "";
    resp->setStatusCode(k200OK);
    resp->setContentTypeCode(CT_TEXT_HTML);
    resp->addHeader("Access-Control-Allow-Origin", "*");
    resp->setBody(resdata);
    callback(resp);
}

void User::mountusb(const HttpRequestPtr &req,
                    std::function<void(const HttpResponsePtr &)> &&callback)
{
    auto resp = HttpResponse::newHttpResponse();
    char buf[1024];
    char command[100];
    char *cmd;
    system("ls /dev/sd* > NO.usb.txt");
    int fd, size;
    fd = open("NO.usb.txt", O_RDONLY);
    size = read(fd, buf, sizeof(buf));
    cmd = strtok(buf, "\n");
    cmd = strtok(NULL, "\n");
    sprintf(command, "echo 20170103 | sudo -S mount %s /mnt", cmd);
    resp->setStatusCode(k200OK);
    resp->setContentTypeCode(CT_TEXT_HTML);
    resp->addHeader("Access-Control-Allow-Origin", "*");
    if (system(command) > 0)
    {
        resp->setBody("true");
    }
    else
    {
        resp->setBody("false");
    }
    close(fd);
    callback(resp);
}